package com.crrc.yzt.demo.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioGroup
import android.widget.RadioGroup.OnCheckedChangeListener
import com.crrc.yzt.demo.base.BaseActivity
import com.crrc.yzt.demo.blue.R
import com.crrc.yzt.demo.blue.databinding.ActivityLocalBinding
import com.crrc.yzt.demo.utils.AppUtils

class LocalActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityLocalBinding.inflate(layoutInflater)
        setContentView(binding.root)


        binding.ivBack.setOnClickListener{
            finish()
        }



        binding.apply {
            var isChinaLocal = isChinaLocal()
            if(!isChinaLocal){
                radio2.isChecked = true
            }
            radioGroup.setOnCheckedChangeListener(object:OnCheckedChangeListener{
                override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
                    when(checkedId){
                        R.id.radio1->{
                            mActivity?.let { AppUtils.toChinaLocal(it) }
                            saveLocal(true)
                        }
                        R.id.radio2->{
                            mActivity?.let { AppUtils.toKoreaLocal(it) }
                            saveLocal(false)
                        }
                    }

                    val intent = Intent(mActivity, LoginActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                    startActivity(intent);
                }

            })
        }
    }

    fun saveLocal(isChina:Boolean){
        val sp = getSharedPreferences("local", Context.MODE_PRIVATE)
        sp?.edit()?.putBoolean("isChina",isChina)?.commit()
    }

    fun isChinaLocal() :Boolean{
        return getSharedPreferences("local", Context.MODE_PRIVATE)
            ?.getBoolean("isChina",true) ?: true
    }

}